﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using QLKS2014.PUBLIC;
using System.Windows.Forms;

namespace QLKS2014.DATA
{
    public class NguoiDungData
    {
        DataConnecter connect;
        public NguoiDungData()
        {
            connect = new DataConnecter();
        }

        //selectAll
        public List<NguoiDungPublic> selectAll()
        {
            try
            {
                DataTable dt = new DataTable();
                List<NguoiDungPublic> list = new List<NguoiDungPublic>();
                NguoiDungPublic nguoiDungPublic = new NguoiDungPublic();

                int row;
                dt = connect.getData("NguoiDung_SelectAll");
                row = dt.Rows.Count;
                for (int i = 0; i < row; i++)
                {
                    nguoiDungPublic.MaNguoiDung = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    nguoiDungPublic.TenNguoiDung = dt.Rows[i].ItemArray[1].ToString();
                    nguoiDungPublic.LoaiNguoiDung = dt.Rows[i].ItemArray[2].ToString();
                    nguoiDungPublic.MatKhau = dt.Rows[i].ItemArray[3].ToString();
                    list.Add(nguoiDungPublic);
                }
                return list;
            }
            catch (Exception ex)
            {
                //bao loi
                MessageBox.Show(ex.Message, "Không Thể Load Dữ Liệu");
                return null;
            }
        }

        //selectByID
        public List<NguoiDungPublic> selectById(NguoiDungPublic nguoiDung)
        {
            try
            {
                DataTable dt = new DataTable();
                List<NguoiDungPublic> list = new List<NguoiDungPublic>();
                

                int param = 1;
                string[] name = new string[param];
                object[] values = new object[param];

                name[0] = "@MaNguoiDung";
                values[0] = nguoiDung.MaNguoiDung;

                int row;
                dt = connect.getData("NguoiDung_SelectById");
                row = dt.Rows.Count;
                for (int i = 0; i < row; i++)
                {
                    NguoiDungPublic nguoiDungPublic = new NguoiDungPublic();
                    nguoiDungPublic.MaNguoiDung = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    nguoiDungPublic.TenNguoiDung = dt.Rows[i].ItemArray[1].ToString();
                    nguoiDungPublic.LoaiNguoiDung = dt.Rows[i].ItemArray[2].ToString();
                    nguoiDungPublic.MatKhau = dt.Rows[i].ItemArray[3].ToString();
                    list.Add(nguoiDungPublic);
                }
                return list;
            }
            catch (Exception ex)
            {
                //bao loi
                MessageBox.Show(ex.Message,"Không Thể Load Dữ Liệu");
                return null;
            }
        }


        //lay thong tin nguoi dung bang username
        public List<NguoiDungPublic> selectByName(NguoiDungPublic nguoiDung)
        {
            try
            {
                DataTable dt = new DataTable();
                List<NguoiDungPublic> list = new List<NguoiDungPublic>();


                int param = 1;
                string[] name = new string[param];
                object[] values = new object[param];

                name[0] = "@TenNguoiDung";
                values[0] = nguoiDung.TenNguoiDung;

                int row;
                dt = connect.getData("NguoiDung_SelectByName",name,values,param);
                row = dt.Rows.Count;
                for (int i = 0; i < row; i++)
                {
                    NguoiDungPublic nguoiDungPublic = new NguoiDungPublic();
                    nguoiDungPublic.MaNguoiDung = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    nguoiDungPublic.TenNguoiDung = dt.Rows[i].ItemArray[1].ToString();
                    nguoiDungPublic.MatKhau = dt.Rows[i].ItemArray[2].ToString();
                    nguoiDungPublic.LoaiNguoiDung = dt.Rows[i].ItemArray[3].ToString();
                    
                    list.Add(nguoiDungPublic);
                }
                return list;
            }
            catch (Exception ex)
            {
                //bao loi
                MessageBox.Show(ex.Message, "Không Thể Load Dữ Liệu");
                return null;
            }
        }


        public void update(NguoiDungPublic nguoiDungPublic)
        {
            int param = 2;
            string[] name = new string[param];
            object[] values = new object[param];

            name[0] = "@MaNguoiDung";
            name[1] = "@MatKhau";

            values[0] = nguoiDungPublic.MaNguoiDung;
            values[1] = nguoiDungPublic.MatKhau;


            connect.ExcuteNonQuery("NguoiDung_Update", name, values, param);
        }

        
    }
}
